import { Scene } from "three";

import Camera from "./Camera";
import Helper from "./Helper";
import Renderer from "./Renderer";
import Size from "./Size";
import Time from "./Time";
import World from "./World/World";

export default class Base {
  public static base: Base;
  public canvas: HTMLCanvasElement;
  public scene: Scene;
  public camera: Camera;
  public size: Size;
  public time: Time;
  public renderer: Renderer;

  public world: World;

  constructor(canvas: HTMLCanvasElement) {
    Base.base = this;

    this.canvas = canvas as HTMLCanvasElement;
    this.scene = new Scene();
    this.size = new Size();
    this.camera = new Camera();
    this.time = new Time();
    this.renderer = new Renderer();
    new Helper();

    this.world = new World();

    this.time.on("update", () => {
      this.update();
    });
    this.size.on("resize", () => {
      this.resize();
    });
  }

  public static getInstance(canvas?: HTMLCanvasElement) {
    if (canvas === undefined && Base.base !== undefined) {
      return Base.base;
    }
    Base.base = new Base(canvas as HTMLCanvasElement);
    return Base.base;
  }

  private resize() {
    this.camera.resize();
    this.renderer.resize();
  }

  private update() {
    this.camera.update();
    this.renderer.update();
  }
}
